#include "SvpSampleWk.h"
#include "SvpSampleCom.h"

#include "SvpSampleYolo.h"
#include "SvpSampleSsd.h"

#include "mpi_nnie.h"

#ifdef USE_OPENCV
#include "cv_draw_rect.h"
#endif

#include "retinanet.h"
#include "math.h"



// sort
void QuickSort(HI_FLOAT *nums, HI_S32 left, HI_S32 right){
	if(left>=right) return;
	if(left > DETECTION_RESULT_NUM_MAX) return;

//	cout << "left: " << left << "   right: " << right << endl;
	HI_S32 i=left, j=right; // i=left+1 is ok
	HI_FLOAT tmp;

	while(i<j){
		// big left, small right
		while(nums[j]<=nums[left] && i<j) j -= 6;
		while(nums[i]>=nums[left] && i<j) i += 6;

		if(i<j){
			for(int u=0; u<6; ++u){
				tmp = nums[i+u];
				nums[i+u] = nums[j+u];
				nums[j+u] = tmp;
			}
		}
	}
	for(HI_S32 u=0; u<6; ++u){
		tmp = nums[i+u];
		nums[i+u] = nums[left+u];
		nums[left+u] = tmp;
	}

	QuickSort(nums, left, i-6);
	QuickSort(nums, i+6, right);
}


HI_FLOAT myClip(HI_FLOAT value){
	HI_FLOAT max = RETINA_INPUT_SIZE - 1;
	value = value > max ? max : value;
	value = value < 0 ? 0 : value;
	return value;
}


HI_S32 retinanet_soft(SVP_NNIE_ONE_SEG_DET_S *pstDetParam, SVP_SOFT_RETINA_S *pstSoftParam, // NNIE param, soft param
		              string& strResultFolderDir, vector<SVP_SAMPLE_FILE_NAME_PAIR>& imgNameRecoder) // save road, img name
{
	HI_S32 s32Ret = HI_SUCCESS;
	HI_S32 image_stride[RETINA_FPN_LAYER_NUM] = {8, 16, 32, 64, 128};
	HI_S32 anchor_size[RETINA_FPN_LAYER_NUM] = {32, 64, 128, 256, 512};
	for(int i=0; i<RETINA_FPN_LAYER_NUM; i++){
		pstSoftParam->image_stride[i] = image_stride[i];
		pstSoftParam->anchor_size[i] = anchor_size[i];
		cout<< i << "th image stride: " << pstSoftParam->image_stride[i]
				 << "   anchor size: " << pstSoftParam->anchor_size[i] << endl;
	}

	HI_FLOAT anchor_scale[3] = {1, 1.26f, 1.587f};
	HI_FLOAT anchor_rate[3] = {0.5f, 1, 2};
	for(int i=0; i<3; i++){
		pstSoftParam->anchor_scale[i] = anchor_scale[i];
		pstSoftParam->anchor_rate[i] = anchor_rate[i];
		cout<< i << "th anchor scale: " << pstSoftParam->anchor_scale[i]
		    << "   anchor rate: " << pstSoftParam->anchor_rate[i] << endl << endl;
	}

	// get NNIE data
	for (HI_U32 i = 0; i < RETINA_FPN_LAYER_NUM * 2; i++) {
		if(i < RETINA_FPN_LAYER_NUM)
			pstSoftParam->detection_input_data[i] = (HI_S32*)((HI_U8*)pstDetParam->astDst[i].u64VirAddr); // loc
		else
			pstSoftParam->softmax_input_data[i - RETINA_FPN_LAYER_NUM] = (HI_S32*)((HI_U8*)pstDetParam->astDst[i].u64VirAddr); // conf
	}

	// get feature shape
	for (HI_U32 i = 0; i < RETINA_FPN_LAYER_NUM; i++){
		pstSoftParam->feature_width[i] = pstDetParam->astDst[i].unShape.stWhc.u32Width;
		pstSoftParam->feature_height[i] = pstDetParam->astDst[i].unShape.stWhc.u32Height;
		pstSoftParam->feature_channel_loc[i] = pstDetParam->astDst[i].unShape.stWhc.u32Chn;
		pstSoftParam->feature_channel_conf[i] = pstDetParam->astDst[i+RETINA_FPN_LAYER_NUM].unShape.stWhc.u32Chn;
		pstSoftParam->feature_stride[i] = pstDetParam->astDst[i].u32Stride;
		cout << i << "th feature shape: w: " << pstSoftParam->feature_width[i]
			 << "   h: " << pstSoftParam->feature_height[i]
			 << "   c_loc: " << pstSoftParam->feature_channel_loc[i]
			 << "   c_conf: " << pstSoftParam->feature_channel_conf[i]
			 << "   s: " << pstSoftParam->feature_stride[i] << endl << endl;
	}

	pstSoftParam->total_detection_num = 0;
	// conf calculate
	for (HI_U32 i = 0; i < RETINA_FPN_LAYER_NUM; i++){
		cout<<"i: " << i<<endl;
		HI_S32* ps32InputData = (HI_S32*)pstSoftParam->softmax_input_data[i];
		HI_S32* ps32InputLoc = (HI_S32*)pstSoftParam->detection_input_data[i];
		for(HI_U32 h=0; h<pstSoftParam->feature_height[i]; h++){ // h
//			cout<<" h: "<< h <<endl;
			for(HI_U32 w=0; w<pstSoftParam->feature_width[i]; w++){ // w
//				cout<<"  w: "<<w<<endl;
				for(HI_U32 anchor=0; anchor<RETINA_PER_ANCHOR_NUM; anchor++){ // anchor
//					cout<<"   anchor: "<<anchor<<endl;
					HI_FLOAT sroce = -9999;
					HI_U32 dclass = 0;
					for(HI_U32 c=0; c<RETINA_LABEL_NUM; c++){
						HI_U32 index = (c+anchor*RETINA_LABEL_NUM)*pstSoftParam->feature_stride[i]*pstSoftParam->feature_height[i]/4 +
									   h*pstSoftParam->feature_stride[i]/4 + w;
						HI_S32* InputSingleData = ps32InputData + index;
						HI_FLOAT confident = InputSingleData[0]*1.0f / SVP_WK_QUANT_BASE;
//						cout<<"    sroce: "<<confident<< "   ps32InputData: " << ps32InputData
//							<< "   InputSingleData: " << InputSingleData << "   index: " << index << endl;
						if(confident > sroce){
							sroce = confident;
							dclass = c;
						}
					}

					// get bbox and save like [sroce class xyxh]
					if(sroce > RETINA_CLASS_THRESHOLD){
//						HI_U32 rate_id = anchor % 3;
//						HI_U32 scale_id = anchor / 3;
						HI_U32 rate_id = anchor / 3;
						HI_U32 scale_id = anchor % 3;

						HI_FLOAT anchor_w = anchor_scale[scale_id] * anchor_size[i] / sqrt(anchor_rate[rate_id]);
						HI_FLOAT anchor_h = anchor_scale[scale_id] * anchor_size[i] * sqrt(anchor_rate[rate_id]);
						HI_FLOAT anchor_x = image_stride[i] * (w + 0.5f);
						HI_FLOAT anchor_y = image_stride[i] * (h + 0.5f);

//						cout << "     w: " << w << "   h: " << h << "   anchor: " << anchor
//							 << "   sroce: " << sroce << "   conf: " << dclass <<endl;
						HI_FLOAT delta[RETINA_LOC_NUM]; // xywh
						for(HI_U32 l=0; l<RETINA_LOC_NUM; l++){
							HI_U32 index = (l+anchor*RETINA_LOC_NUM)*pstSoftParam->feature_stride[i]
									     * pstSoftParam->feature_height[i]/4
										 + h*pstSoftParam->feature_stride[i]/4 + w;
							HI_S32* InputSingleLoc = ps32InputLoc + index;
							delta[l] = InputSingleLoc[0]*1.0f / SVP_WK_QUANT_BASE;
						}
						delta[0] *= 0.1f;
						delta[1] *= 0.1f;
						delta[2] *= 0.2f;
						delta[3] *= 0.2f;

						HI_FLOAT bbox_x = anchor_x + delta[0] * anchor_w;
						HI_FLOAT bbox_y = anchor_y + delta[1] * anchor_h;
						HI_FLOAT bbox_w = exp(delta[2]) * anchor_w;
						HI_FLOAT bbox_h = exp(delta[3]) * anchor_h;

						cout << "     w: " << w << "   h: " << h << "   anchor: " << anchor
							 << "   sroce: " << sroce << "   conf: " << dclass << "   num: " << pstSoftParam->total_detection_num
							 << "   bx: " << bbox_x << "   by: " << bbox_y
							 << "   bw: " << bbox_w << "   bh: " << bbox_h << endl;

						if(pstSoftParam->total_detection_num >= SAVE_RESULT_NUM_MAX){
							cout << "Warning! total_detection_num out of memory" << endl;
							break;
						}

						pstSoftParam->ps32ResultMem[pstSoftParam->total_detection_num*6] = sroce;
						pstSoftParam->ps32ResultMem[pstSoftParam->total_detection_num*6 + 1] = dclass;
						pstSoftParam->ps32ResultMem[pstSoftParam->total_detection_num*6 + 2] = bbox_x - bbox_w/2; // x
						pstSoftParam->ps32ResultMem[pstSoftParam->total_detection_num*6 + 3] = bbox_y - bbox_h/2; // y
						pstSoftParam->ps32ResultMem[pstSoftParam->total_detection_num*6 + 4] = bbox_x + bbox_w/2; // x
						pstSoftParam->ps32ResultMem[pstSoftParam->total_detection_num*6 + 5] = bbox_y + bbox_h/2; // y

						pstSoftParam->total_detection_num ++;
					}
				} // anchor
			} // w
		} // h
	} // stage

	cout << "detect num: " << pstSoftParam->total_detection_num << "    before sort sroce: " << endl;
	for(HI_U32 ii=0; ii<pstSoftParam->total_detection_num; ++ii)
		cout << "ii: " << ii << "   score: " << pstSoftParam->ps32ResultMem[ii*6] << endl;

	QuickSort(pstSoftParam->ps32ResultMem, 0, pstSoftParam->total_detection_num*6-6); // sort
	cout << endl << "has " << pstSoftParam->total_detection_num << " detect result, sroce sort:" << endl;
	for(HI_U32 ii=0; ii<pstSoftParam->total_detection_num; ++ii)
		cout << pstSoftParam->ps32ResultMem[ii*6] << endl;

	pstSoftParam->final_detection_num = 0;
	cout << endl << "start nms" << endl;

	memset(pstSoftParam->is_suppression, 0, pstSoftParam->u32SuppressionSize);
	// nms for every class
	for(HI_S32 ii=0; ii<RETINA_LABEL_NUM; ii++){ // for every class
		cout << " ii: " << ii << endl;
		for(HI_U32 jj=0; jj<pstSoftParam->total_detection_num; ++jj){
			if(int(pstSoftParam->ps32ResultMem[jj*6 + 1]) != ii || pstSoftParam->is_suppression[jj] == true)
				continue;

			cout << "  jj: " << jj << endl;

			pstSoftParam->is_suppression[jj] = true;
			for(HI_U32 ll=0; ll<6; ++ll){
				pstSoftParam->ps32FinalBbox[pstSoftParam->final_detection_num*6 + ll]
											= pstSoftParam->ps32ResultMem[jj*6 + ll];
			}
			pstSoftParam->final_detection_num ++;

			for(HI_U32 kk=jj+1; kk<pstSoftParam->total_detection_num; ++kk){
				cout << "   kk: " << kk << endl;
				if(int(pstSoftParam->ps32ResultMem[kk*6 + 1]) != ii || pstSoftParam->is_suppression[kk] == true)
					continue;
				cout << " ii: " << ii << " jj: " << jj << " kk: " << kk << endl;

				HI_FLOAT xj1 = pstSoftParam->ps32ResultMem[jj*6 + 2];
				HI_FLOAT yj1 = pstSoftParam->ps32ResultMem[jj*6 + 3];
				HI_FLOAT xj2 = pstSoftParam->ps32ResultMem[jj*6 + 4];
				HI_FLOAT yj2 = pstSoftParam->ps32ResultMem[jj*6 + 5];

				HI_FLOAT xk1 = pstSoftParam->ps32ResultMem[kk*6 + 2];
				HI_FLOAT yk1 = pstSoftParam->ps32ResultMem[kk*6 + 3];
				HI_FLOAT xk2 = pstSoftParam->ps32ResultMem[kk*6 + 4];
				HI_FLOAT yk2 = pstSoftParam->ps32ResultMem[kk*6 + 5];

				HI_FLOAT overlap_top_left_x = max(xj1, xk1);
				HI_FLOAT overlap_top_left_y = max(yj1, yk1);
				HI_FLOAT overlap_bot_right_x = min(xj2, xk2);
				HI_FLOAT overlap_bot_right_y = min(yj2, yk2);
				HI_FLOAT overlap_area;
				if((overlap_bot_right_x - overlap_top_left_x < 0) || (overlap_bot_right_y - overlap_top_left_y < 0))
					overlap_area = 0;
				else
					overlap_area = (overlap_bot_right_x - overlap_top_left_x) * (overlap_bot_right_y - overlap_top_left_y);

				HI_FLOAT union_area = (yk2 - yk1)*(xk2 - xk1) + (yj2 - yj1)*(xj2 - xj1) - overlap_area;
				HI_FLOAT IoU = overlap_area / union_area+0.0001f;

				cout << "  over: " << overlap_area << "  union: " << union_area << "  IoU: " << IoU ;
				if(IoU > NMS_IOU_THRESHOLD){
					pstSoftParam->is_suppression[kk] = true;
					cout << "   is suppression" << endl;
				}
				else
					cout << endl;
			}
		}
	}

	cout << endl << "has " << pstSoftParam->final_detection_num << " nms result:" << endl;
	for(HI_U32 ii=0; ii<pstSoftParam->final_detection_num; ++ii){
		cout << "   sroce: " << pstSoftParam->ps32FinalBbox[ii*6] << "   conf: " << pstSoftParam->ps32FinalBbox[ii*6+1]
			 << "   x1: " << pstSoftParam->ps32FinalBbox[ii*6+2] << "   y1: " << pstSoftParam->ps32FinalBbox[ii*6+3]
			 << "   x2: " << pstSoftParam->ps32FinalBbox[ii*6+4] << "   y2: " << pstSoftParam->ps32FinalBbox[ii*6+5] << endl;
	}

	// draw rect
	cout << endl << "final result: " << endl;
//	if(pstSoftParam->final_detection_num > 5)
//		pstSoftParam->final_detection_num = 5;
	vector < svpsample_utils_cv::SVPUtils_TaggedBox_S> vTaggedBoxes;
	for (HI_U32 i = 0; i < pstSoftParam->final_detection_num; i++){
		if(pstSoftParam->ps32FinalBbox[i*6+0] < SHOW_CLASS_THRESHOLD)
			continue;
		for(HI_U32 j = 0; j < 4; ++j){
			pstSoftParam->ps32FinalBbox[i*6+j+2] = myClip(pstSoftParam->ps32FinalBbox[i*6+j+2]);
			cout << " " << pstSoftParam->ps32FinalBbox[i*6+j+2];
		}cout << endl;
		svpsample_utils_cv::SVPUtils_TaggedBox_S stTaggedBox = { 0 };
		stTaggedBox.stRect.x = (HI_U32)pstSoftParam->ps32FinalBbox[i*6+2]; // x1
		stTaggedBox.stRect.y = (HI_U32)pstSoftParam->ps32FinalBbox[i*6+3]; // y1
		stTaggedBox.stRect.w = (HI_U32)(pstSoftParam->ps32FinalBbox[i*6+4] - pstSoftParam->ps32FinalBbox[i*6+2]); // w
		stTaggedBox.stRect.h = (HI_U32)(pstSoftParam->ps32FinalBbox[i*6+5] - pstSoftParam->ps32FinalBbox[i*6+3]); // h
		stTaggedBox.fScore = pstSoftParam->ps32FinalBbox[i*6+0];
		stTaggedBox.u32Class = pstSoftParam->ps32FinalBbox[i*6+1];
		vTaggedBoxes.push_back(stTaggedBox);
	}

	string strBoxedImgPath = imgNameRecoder[0].first + "_det.png";
	strBoxedImgPath = strResultFolderDir + strBoxedImgPath;
	s32Ret = svpsample_utils_cv::SVPUtils_DrawBoxes(
		pstDetParam->astSrc,
		svpsample_utils_cv::SVPUtils_GetImgType(pstDetParam->astSrc->enType),
		strBoxedImgPath.c_str(),
		vTaggedBoxes,
		0);

	return s32Ret;
}



HI_S32 myReadAllSrcImg(FILE *afp[], SVP_SRC_BLOB_S astSrcBlobs[],
    HI_U32 u32SrcNum, vector<SVP_SAMPLE_FILE_NAME_PAIR>& imgNameRecoder)
{
    HI_S32 s32Ret = HI_SUCCESS;
    static int file_num = 0;
    imgNameRecoder.clear();

    for (HI_U32 j = 0; j < u32SrcNum; j++)
    {
        vector<SVP_SAMPLE_FILE_NAME_PAIR> imgNameRecoder_temp;

        if (astSrcBlobs[j].enType >= 0 && astSrcBlobs[j].enType < SVP_BLOB_TYPE_BUTT)
        {
            FILE *fp = afp[j];
            s32Ret = SvpSampleImgReadFromImglist(fp, &astSrcBlobs[j], file_num, imgNameRecoder_temp);
//            s32Ret = myImgReadFromImglist(fp, &astSrcBlobs[j], file_num, imgNameRecoder_temp);

            CHECK_EXP_RET(HI_SUCCESS != s32Ret, s32Ret, "SvpSampleImgReadFromImglist failed");
        }
        /* get image name only from src[0] image list */
        if (0 == j) {
            imgNameRecoder = imgNameRecoder_temp;
        }

//        for(int i = 0; i<imgNameRecoder.size(); ++i){
//        	cout << i << ": " << imgNameRecoder[i].first << "   " << imgNameRecoder[i].second << endl;
//        }
        file_num ++;
    }
    return s32Ret;
}


HI_S32 Retinanet()
{
	SVP_NNIE_CFG_S stDetCfg = { 0 }; // cfg inform
	SVP_NNIE_ONE_SEG_DET_S stDetParam = { 0 }; // NNIE infor
	SVP_SOFT_RETINA_S stSoftParam = {0}; // soft infor
	HI_S32 s32Ret = HI_SUCCESS;

//	const HI_CHAR *pszModelName = "../../data/detection/retinanet/res_func20.wk";
//	const HI_CHAR *pszModelName = "../../data/detection/retinanet/mbl_20_func.wk";
//	const HI_CHAR *pszModelName = "../../data/detection/retinanet/res_nobn_20_func.wk";
	const HI_CHAR *pszModelName = "../../data/detection/retinanet/mbl_nobn_20_func.wk";
	const HI_CHAR *paszPicList[] = { "../../data/detection/retinanet/image_test_list.txt"};

	stDetCfg.pszModelName = pszModelName; // wk road
	memcpy(&stDetCfg.paszPicList, paszPicList, sizeof(HI_VOID*)*1);
	stDetCfg.u32MaxInputNum = SVP_NNIE_MAX_INPUT_NUM;
	stDetCfg.u32MaxBboxNum = 0;

	// mkdir to save result, name folder by model type
	string strNetType = "retinanet";
	string strResultFolderDir = "result_" + strNetType + "/";
	s32Ret = SvpSampleMkdir(strResultFolderDir.c_str());
	CHECK_EXP_RET(HI_SUCCESS != s32Ret, s32Ret, "SvpSampleMkdir(%s) failed", strResultFolderDir.c_str());

	HI_U32 u32MaxInputNum = SVP_NNIE_MAX_INPUT_NUM;
	HI_U32 u32Batch   = 0; // batch size
	HI_U32 u32LoopCnt = 0; // goal num of a img
	HI_U32 u32StartId = 0;

	/***************** NNIE init ****************/
	s32Ret = SvpSampleOneSegDetCnnInit(&stDetCfg, &stDetParam); // config stDetParam about NNIE para
	CHECK_EXP_RET(HI_SUCCESS != s32Ret, s32Ret, "SvpSampleOneSegCnnInit failed");

//	cout << "NNIE out segment num: " << stDetParam.stModel.astSeg->u16DstNum << endl;
//	for (HI_U32 i = 0; i < stDetParam.stModel.astSeg->u16DstNum; i++)
//	{
//		if (i < stDetParam.stModel.astSeg->u16DstNum / 2) { // location 0
//			cout << i << "th location blob"
//				 << "    w:" << stDetParam.astDst[i].unShape.stWhc.u32Width
//				 << "    h:" << stDetParam.astDst[i].unShape.stWhc.u32Height
//				 << "    c:" << stDetParam.astDst[i].unShape.stWhc.u32Chn
//				 << "    stride:" << stDetParam.astDst[i].u32Stride << endl;
//		}
//		else { // confidence 1
//			cout << i - stDetParam.stModel.astSeg->u16DstNum/2 << "th confidence blob"
//				 << "    w:" << stDetParam.astDst[i].unShape.stWhc.u32Width
//				 << "    h:" << stDetParam.astDst[i].unShape.stWhc.u32Height
//				 << "    c:" << stDetParam.astDst[i].unShape.stWhc.u32Chn
//				 << "    stride:" << stDetParam.astDst[i].u32Stride << endl;
//		}
//	}

	/***************** soft init ****************/
	stSoftParam.u32ResultMemSize = SAVE_RESULT_NUM_MAX * 6 * sizeof(HI_FLOAT);
	stSoftParam.u32SuppressionSize = SAVE_RESULT_NUM_MAX * sizeof(bool);
	stSoftParam.u32FinalBboxSize = DETECTION_RESULT_NUM_MAX * 6 * sizeof(HI_FLOAT);
//	cout << "sizeof(HI_FLOAT): " << sizeof(HI_FLOAT) << endl;

	stSoftParam.ps32ResultMem = (HI_FLOAT*)malloc(stSoftParam.u32ResultMemSize
			                  + stSoftParam.u32SuppressionSize + stSoftParam.u32FinalBboxSize);
	stDetParam.ps32ResultMem = (HI_S32*)stSoftParam.ps32ResultMem;  // [sroce class x y x y]
	stSoftParam.is_suppression = (bool *)(stSoftParam.ps32ResultMem + stSoftParam.u32ResultMemSize / sizeof(HI_FLOAT)); // nms flag
	stSoftParam.ps32FinalBbox = (HI_FLOAT *)(stSoftParam.is_suppression + stSoftParam.u32SuppressionSize / sizeof(bool)); // final bbox
//	cout << "stSoftParam.ps32ResultMem: " << stSoftParam.ps32ResultMem << "   stSoftParam.u32ResultMemSize: "
//		 << stSoftParam.u32ResultMemSize << "   stSoftParam.is_suppression: " << stSoftParam.is_suppression << endl;

	if (HI_NULL != stSoftParam.ps32ResultMem)
		memset(stSoftParam.ps32ResultMem, 0,
			   stSoftParam.u32ResultMemSize + stSoftParam.u32SuppressionSize + stSoftParam.u32FinalBboxSize);

	// calc batch loop count
	u32Batch = SVP_SAMPLE_MIN(1, stDetParam.u32TotalImgNum);
	u32Batch = SVP_SAMPLE_MIN(u32Batch, stDetParam.astSrc[0].u32Num);
	if (0 == u32Batch) {
		s32Ret = HI_FAILURE;
		CHECK_EXP_GOTO(HI_TRUE, Fail,
			"u32Batch = 0 failed! u32MaxInputNum(%d), stDetParam.u32TotalImgNum(%d), astSrc[0].u32Num(%d)",
			u32MaxInputNum, stDetParam.u32TotalImgNum, stDetParam.astSrc[0].u32Num);
	}

	u32LoopCnt = stDetParam.u32TotalImgNum / u32Batch; // process nums
	stDetParam.astSrc->u32Num = 1; // use to test mutiply imgs
	/**************************************************************************/
	/* 4. run forward and detection */
	// run batch size imgs
	for (HI_U32 i = 0; i < u32LoopCnt; i++)
	{
		/* init vector mem with size u32Batch */
		vector<SVP_SAMPLE_FILE_NAME_PAIR> imgNameRecoder;

		// read img
		s32Ret = myReadAllSrcImg(stDetParam.fpSrc, stDetParam.astSrc, stDetParam.stModel.astSeg[0].u16SrcNum, imgNameRecoder);
		CHECK_EXP_GOTO(HI_SUCCESS != s32Ret, Fail, "Error(%#x):SvpSampleReadAllSrcImg failed!", s32Ret);
		if (imgNameRecoder.size() != u32Batch) {
			s32Ret = HI_FAILURE;
			CHECK_EXP_GOTO(HI_TRUE, Fail,
				"Error(%#x):imgNameRecoder.size(%d) != u32Batch(%d)", HI_FAILURE, (HI_U32)imgNameRecoder.size(), u32Batch);
		}

		// run NNIE
		s32Ret = SvpSampleCnnDetectionForword(&stDetParam, &stDetCfg); // NNIE
		CHECK_EXP_GOTO(HI_SUCCESS != s32Ret, Fail, "SvpSampleCnnDetectionForword failed");
		cout << i << "th loop" << endl;
		for(HI_U32 ii=0; ii<u32Batch; ii++){
			cout << ii << "th img info:  " << imgNameRecoder[ii].first << "   " << imgNameRecoder[ii].second << endl;
		}

		s32Ret = retinanet_soft(&stDetParam, &stSoftParam, strResultFolderDir, imgNameRecoder);

		u32StartId += u32Batch;
	}

	/**************************************************************************/
	/* 5. deinit resources */
Fail:
	SvpSampleOneSegDetCnnDeinit(&stDetParam); // deinit NNIE para
//	SvpSampleMemFree(stSoftParam.ps32ResultMem);
	return s32Ret;
}

